﻿using LEDStripMixer.Views;
using LEDStripMixer.ViewModels;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using LEDStripMixer.Views.Configuration;
using LEDStripMixer.Shared.Extensions;
using System.Windows.Media.Effects;

namespace LEDStripMixer
{
	/// <summary>
	/// Interaction logic for Configuration.xaml
	/// </summary>
	public partial class Configuration : Window, IConfiguration
	{
        public ConfigurationPresenter Presenter { get; set; }

		public Configuration()
		{
			this.InitializeComponent();

            Presenter = new ConfigurationPresenter(this);
            Presenter.Initialise();
		}

        #region Public Methods

        public void BindConfigurationLookups(ConfigurationLookups lookups)
        {
            try
            {
                drpCOMPort.Items.Clear();
                drpBaudRate.Items.Clear();
                lstFPS.Items.Clear();
            }
            catch { }

            if (lookups.IsValid())
            {
                drpCOMPort.ItemsSource = lookups.COMPorts;
                drpBaudRate.ItemsSource = lookups.BaudRates;
                lstFPS.ItemsSource = lookups.FPS;

                drpCOMPort.SelectedIndex = 0;
                drpBaudRate.SelectedIndex = 0;
                lstFPS.SelectedIndex = 0;

                drpCOMPort.IsEnabled = true;
                drpBaudRate.IsEnabled = true;
                lstFPS.IsEnabled = true;
                btnSave.IsEnabled = true;
            }
            else
            {
                drpCOMPort.IsEnabled = false;
                drpBaudRate.IsEnabled = false;
                lstFPS.IsEnabled = false;
                btnSave.IsEnabled = false;
            }
        }

        public void BindConfiguration(ConfigurationBindModel model)
        {
            if (model.BaudRate.IsPositiveInteger())
            {
                drpBaudRate.SelectedValue = model.BaudRate;
            }

            if (model.COMPort.IsNotNullOrEmpty())
            {
                drpCOMPort.SelectedValue = model.COMPort;
            }

            if (model.FPS.IsPositiveInteger())
            {
                lstFPS.SelectedValue = model.FPS;
            }
        }

        public void Show()
        {
            ((Window)this).WindowStartupLocation = WindowStartupLocation.CenterScreen;
            ((Window)this).Show();
        }

        public void Hide()
        {
            ((Window)this).Hide();
        }

        #endregion

        #region Events

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            var model = new ConfigurationBindModel()
            {
                BaudRate = (Int32)drpBaudRate.SelectedValue,
                COMPort = (string)drpCOMPort.SelectedValue,
                FPS = (Int32)lstFPS.SelectedValue
            };

            Presenter.SaveConfig(model);
        }

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            Presenter.LoadConfig();
        }

        #endregion

    }
}